package com.nbsaas.boot.controller.web;

import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.MapResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.thread.TenantContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class MessageController {


    @Autowired
    @Qualifier("tenantTaskExecutor")
    private ThreadPoolTaskExecutor executor;

    public void submitTask() {
        //TenantContext.setTenant("tenant-001");
        executor.execute(() -> {
            System.out.println("Running for tenant: " + TenantContext.getTenant());
        });

        Future<String> temp = executor.submit(() -> {
            System.out.println("Running for tenant: " + TenantContext.getTenant());
            return "Task completed";
        });
        try {
            String xx= temp.get();
            System.out.println(xx);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("sendMessage")
    public ResponseObject<?> sendMessage(String client,String message) {
        ResponseObject<?> result=new ResponseObject<>();
        return result;
    }

    @RequestMapping("list")
    public ListResponse<MapResponse> list() {
      ListResponse<MapResponse> result=new ListResponse<>();
        result.setData(new ArrayList<>());


        return result;
    }

    @RequestMapping("/doTest")
    public ResponseObject<?> doTest(String sessionId) {
        ResponseObject<?> result = new ResponseObject<>();
//        orderInfoApi.listData(Filter.and(Filter.ge("id",1)),Filter.le("id",10),
//                Filter.or(Filter.ge("id",10000)),Filter.le("id",10003)
//                );
        submitTask();
        return result;
    }

}
